package com.hzdl.controller;

import com.hzdl.bean.Room;
import com.hzdl.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * author:Administrator
 * date:2018\9\5 0005
 * description:
 */
@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;

    //获取某一项目下的所有房间资源
    @RequestMapping(value = "/getAllRoomsByPid")
    public String getAllRoomsByPid(int pid,Model model,HttpSession session){
        //String pid = request.getParameter("pid");
        //int pid = 1;
        System.err.println("++++++++++++++++"+pid);
        List<Room> rooms = roomService.getAllRoomsByPid(pid);
        //System.err.println(rooms);
        session.setAttribute("pid",pid);
        model.addAttribute("rooms",rooms);
        return "room";
    }
    //获取待出租的房间信息
    @RequestMapping(value = "/getAllRooms")
    public String getAllRooms(Model model){
        List<Room> assetDetails = roomService.getAllRooms(0);
        //System.err.println(assetDetails);
        model.addAttribute("assetDetails",assetDetails);
        return "resource";
    }
    //跳转编辑资源界面
    @RequestMapping(value = "/toEditRoom")
    public String toEditRoom(int rid,Model model){


//        String rid = request.getParameter("rid");
//        request.setAttribute("rid2",rid);
        Room room = roomService.getRoomByRid(rid);
        model.addAttribute("room",room);
        return "room_edit";
    }

    //编辑资源
    @RequestMapping(value = "/editRoom")
    public String editRoom(Room room){

        roomService.editRoom(room.getRname(),room.getSize(),room.getRent_size(),room.getRid());
        //System.out.println("修改后的房间信息："+room);
        return "forward:/room/getAllRoomsByPid";
    }

    //跳转添加资源界面
    @RequestMapping(value = "/toAddRoom")
    public String toAddRoom(){


        return "room_add";
    }

    //添加资源
    @RequestMapping(value = "/addRoom",method = RequestMethod.POST)
    public String addRoom(String rname,double size,double rent_size,HttpSession session){
        int pid= (int)session.getAttribute("pid");
        Room room = new Room(rname,size,rent_size,pid);
        roomService.addRoom(pid,room);
        //System.out.println(room);
        session.setAttribute("room",room);
        return "forward:/room/getAllRoomsByPid?pid="+pid;
    }
}
